﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Forms;

namespace UAS5.UControls
{
    // 2013-6-20 by sutroon
    public class CommandHelper
    {
        public Action<string> OuterCommandHandler { get; set; }
        public void Run(string cmd)
        {
            if(cmd.Length==0) return;
            switch (cmd)
            {
                case "exit":
                    Application.Exit();
                    break;
                case "help":
                    StringBuilder sbAbout = new StringBuilder();
                    sbAbout.AppendLine("help:帮助");
                    sbAbout.AppendLine("time:查看当前时间信息");
                    sbAbout.AppendLine("version:查看程序版本信息");
                    sbAbout.AppendLine("projectnote:查看项目备注");
                    sbAbout.AppendLine("fy {words}:翻译文字");
                    MessageBox.Show(sbAbout.ToString(), "帮助");
                    break;
                case "s":
                    System.Diagnostics.Process.Start("UCL.Forms.ScreenSnap.exe");
                    break;
                case "time":
                    MessageBox.Show(string.Format("{1} {2}{0}一年中第{3}天,第{4}周", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Today.DayOfWeek, DateTime.Today.DayOfYear, DateTime.Today.GetWeekOfYear()), "时间");
                    break;
                case "version":
                    MessageBox.Show(string.Format("Ver 1.0.0 20130608{0}Copyright (c) sutroon{0}Email 14507247@qq.com", Environment.NewLine), "版本信息");
                    break;
                default:
                    AdvCmd(cmd);
                    break;
            }
        }
        private void AdvCmd(string cmdtext)
        {
            string cmd = cmdtext.GetFirstWord().ToLower();
            switch (cmd)
            {
                case "fy":
                    new frmTranslate() { Words = cmdtext.Substring(3), AutoRun = true }.Show();
                    return;

            }
            // 系统命令
            try
            {
                System.Diagnostics.Process.Start(string.Format("{0}\\{1}", Environment.SystemDirectory, cmdtext));
            }
            catch (Exception ex)
            {
                if (OuterCommandHandler != null)
                {
                    try
                    {
                        OuterCommandHandler(cmdtext);
                        return;
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message);
                    }
                }
                MessageBox.Show(ex.Message);
            }
        }
    }
}
